angular.module('app')

.factory('StockService', ['$http','$cacheFactory', function($http,$cacheFactory) {
	//var stockListCache = $cacheFactory("stocklist");
	return {
		getStockNumByType: function(type) { 
			console.log("GET getStockNumByType.es");
			return $http({
				method: 'GET',
				url: 'getStockNumByType.es',
				params: {type: type},
			})
		},
		getAllStocksByType: function(type, currentPage, itemsPerPage) { 
			//var results = stockListCache.get("stocklist");
			//if (results) {
			//	return results;
			//}
			console.log("GET stocklist_json.es?type="+type + "currentPage="+currentPage + "itemsPerPage="+itemsPerPage);
			return $http({
				method: 'GET',
				url: 'stocklist_json.es',
				params: {type: type, currentPage: currentPage, itemsPerPage: itemsPerPage},
				//cache: stockListCache
			})
		},
		saveStockbyCode: function(code) { 
			console.log("GET stock/"+code+"_json.es");
			return $http({
				method: 'GET',
				url: 'stock/'+code+'_json.es',
			})
		},
		
		// category list page
		findStocksByCategory: function(stockCategoryId) { 
			console.log("GET findStocksByCategory_json.es?stockCategoryId="+stockCategoryId);
			return $http({
				method: 'GET',
				url: 'findStocksByCategory_json.es',
				params: {stockCategoryId: stockCategoryId}
			})
		},
		
		saveStockToCategory: function(code, stockCategoryId) { 
			console.log("GET saveStockToCategory.es?code="+code+"&stockCategoryId="+stockCategoryId);
			return $http({
				method: 'GET',
				url: 'saveStockToCategory.es',
				params: {code: code, stockCategoryId: stockCategoryId}
			})
		},
		
		deleteStockById: function(stockId) { 
			console.log("GET deleteStockById.es?id="+stockId);
			return $http({
				method: 'GET',
				url: 'deleteStockById.es',
				params: {stockId: stockId}
			})
		},
		
		
		////////// Self-Select stock related
		
		getSelfSelectNum: function() { 
			console.log("GET getSelfSelectNum.es");
			return $http({
				method: 'GET',
				url: 'getSelfSelectNum.es',
			})
		},
		findAllSelfSelects: function(currentPage, itemsPerPage) { 
			console.log("GET findAllSelfSelects.es");
			return $http({
				method: 'GET',
				url: 'findAllSelfSelects.es',
				params: {currentPage: currentPage, itemsPerPage: itemsPerPage},
			})
		},
		getCurrentPrices: function(selfSelects) { 
			console.log("POST getCurrentPrices.es");
			return $http({
				method: 'POST',
				url: 'getCurrentPrices.es',
				data: selfSelects,
			})
		},
		saveSelfSelectByCode: function(selfSelectCode) { 
			console.log("GET saveSelfSelectByCode.es");
			return $http({
				method: 'GET',
				url: 'saveSelfSelectByCode.es',
				params: {code: selfSelectCode},
			})
		},
		deleteSelfSelectById: function(stockId) { 
			console.log("GET deleteSelfSelectById.es?id="+stockId);
			return $http({
				method: 'GET',
				url: 'deleteSelfSelectById.es',
				params: {stockId: stockId}
			})
		},
	};
}])